home *** CD-ROM | disk | FTP | other *** search
/ Internet Info 1994 March / Internet Info CD-ROM (Walnut Creek) (March 1994).iso / networking / terms / kermit / b / xxu.c < prev   
Encoding:
C/C++ Source or Header  |  1988-12-12  |  4.5 KB  |  138 lines

  1. /*  X X U  --  20-to-Unix filename converter  */
  2.  
  3. /*
  4.  Change DEC-20 or VAX/VMS style filenames into normal Unix names.
  5.  Handy for use after ftp MGETs, when you find your directory full of
  6.  files with names like LIB:<KERMIT>CKUFIO.C.2 or FRED::[ETHEL]A.B;37
  7.  when all you really wanted was ckufio.c and a.b.
  8.  
  9.  Usage: xxu file(s)
  10.  
  11.  Action: Renames argument files as follows:
  12.    strips Unix path name from front (up to rightmost '/') if present
  13.    strips DEC device:, node:: names from front (up to rightmost ':') if present
  14.    strips DEC-20 <directory> or VMS [directory] name if present
  15.    strips DEC-20 version number from end (everything after 2nd dot) if present
  16.    strips VMS generation number from end (everything after ';') if present
  17.    lowercases any uppercase letters
  18.    honors DEC-20 CTRL-V quote for special characters
  19.    discards unquoted unprintable characters
  20.    if result is null, file is renamed to xxfile-n, where n is a number.
  21.    if result would write over an existing file, file also renamed to xxfile-n.
  22.  
  23.  Recommended procedure: make a new directory, cd to it, then FTP files
  24.  from DEC-20 or VMS system, then do "xxu *".
  25.  
  26.  Author:  F. da Cruz, CUCCA, July 85
  27. */
  28.  
  29. #include <stdio.h>
  30. #if    SYSV | M_XENIX
  31. #include <sys/types.h>
  32. #endif
  33. #include <ctype.h>
  34. #include <sys/file.h>            /* For access() */
  35. /* <<<<<<<< define NO_RENAME on cc line if missing >>>>>>>> */
  36.  
  37. char name[500];                /* File name buffer */
  38. char *pp, *cp, *xp;            /* Character pointers */
  39. char delim;                /* Directory Delimiter */
  40. int dc = 0, n = 0;            /* Counters */
  41. int quote = 0, indir = 0; done = 0;    /* Flags */
  42.  
  43. main(argc,argv) int argc; char **argv; {
  44.  
  45.     if (argc < 2) {            /* Give message if no args */
  46.     fprintf(stderr,"Usage: xxu file(s)\n");
  47.     exit(1);
  48.     }
  49.     n = 0;                /* Unfixable filename counter */
  50.     while (--argc > 0) {        /* For all files on command line... */
  51.     argv++;
  52.     xp = *argv;            /* Copy pointer for simplicity */
  53.     printf("%s ",*argv);        /* Echo name of this file */
  54.  
  55.     pp = name;            /* Point to translation buffer */
  56.     *name = '\0';            /* Initialize buffer */
  57.     dc = 0;                /* Filename dot counter */
  58.     done = 0;            /* Flag for early completion */
  59.  
  60.     for (cp = xp; (*cp != '\0') && !done; cp++) { /* Loop thru chars... */
  61.  
  62.         if (quote) {        /* If this char quoted, */
  63.         *pp++ = *cp;        /*  include it literally. */
  64.         quote = 0;
  65.         }
  66.         else if (indir) {        /* If in directory name, */
  67.         if (*cp == delim) indir = 0; /* look for end delimiter. */
  68.         }
  69.         else switch (*cp) {
  70.         case '<':        /* Discard DEC-20 directory name */
  71.             indir = 1;
  72.             delim = '>';
  73.             break;
  74.         case '[':        /* Discard VMS directory name */
  75.             indir = 1;
  76.             delim = ']';
  77.             break;
  78.         case '/':        /* Discard Unix path name */
  79.         case ':':               /*  or DEC dev: or node:: name */
  80.             pp = name; 
  81.             break;
  82.         case '.':        /* DEC -20 generation number */
  83.                 if (++dc == 1)    /* Keep first dot */
  84.                 *pp++ = *cp;
  85.             else        /* Discard everything starting */
  86.                 done = 1;    /* with second dot. */
  87.             break;
  88.         case ';':        /* VMS generation or DEC-20 attrib */
  89.             done = 1;        /* Discard everything starting with */
  90.             break;        /* semicolon */
  91.             case '\026':        /* Control-V quote for special chars */
  92.             quote = 1;        /* Set flag for next time. */
  93.             break;
  94.         default:
  95.             if (isupper(*cp))      /* Uppercase letter to lowercase */
  96.                     *pp++ = tolower(*cp);
  97.             else if (*cp == ' ')/* change blanks to underscore */
  98.                 *pp++ = '_';
  99.             else if (isprint(*cp)) /* Other printable, just keep */
  100.                 *pp++ = *cp;
  101.         }
  102.     }
  103.     *pp = '\0';            /* Done with name, terminate it */
  104.     if (strcmp(name,xp) == 0) {    /* If no renaming necessary, */
  105.         printf("(ok)\n");        /*  just give message. */
  106.         continue;
  107.         }
  108.     while (*name == '\0' || access(name,0) == 0) { /* Find unique name */
  109.         sprintf(name,"xxfile-%d",n++);
  110.     }
  111.     printf("=> %s ",name);        /* Tell what new name will be */
  112.     if (rename(xp,name) == 0)    /* Try to rename it */
  113.         printf("(ok)\n");        /* Say what happened */
  114.     else
  115.         perror("failed");
  116.     }
  117.     exit(0);                /* Done. */
  118. }
  119.  
  120. /*****************************************************************
  121.  |  rename - for systems lacking the rename system call
  122.  |----------------------------------------------------------------
  123.  |  Arguments:
  124.  |   1) string - current filename
  125.  |   2) string - new filename
  126.  ****************************************************************/
  127.  
  128. #if    NO_RENAME
  129. rename(oldname, newname)
  130.     char *oldname, *newname;
  131. {
  132.     char cmdline[133];
  133.     
  134.     sprintf(cmdline, "mv \"%s\" %s", oldname, newname);
  135.     return system(cmdline);
  136. }
  137. #endif
  138.